package com.clw.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by chengliwei on 2018/12/14.
 */
public class FileUtil {

    public static byte[] read(String filePath) throws IOException {
        File file = new File(filePath);
        Long fileLength = file.length();
        byte[] fileContent = new byte[fileLength.intValue()];
        if (file.exists() && file.canRead()) {
            InputStream in = new FileInputStream(file);
            in.read(fileContent);
            in.close();
        }
        return fileContent;
    }

    public static boolean exists(String jreOption) {
        Path path = Paths.get(jreOption);
        return Files.exists(path);
    }

    public static List<String> walk(String baseDir) {
        List<String> result = new ArrayList<>();
        Path path = Paths.get(baseDir);
        try (DirectoryStream<Path> ds = Files.newDirectoryStream(path)) {
            ds.forEach(e -> result.add(baseDir + e.getFileName().toString()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }
}
